<?php

namespace app\api\controller;


class Fx extends Base
{
    public function goods_lists(){
        $goods_id_lists=[];
        $goods_list=model("fxGoods")->field("goods.id,goods.pic,goods.name,goods.price,goods.sales")->join("goods","fx_goods.goods_id=goods.id","left")->select();

        return s($goods_list);
    }
    public function user_get(){
        $user=$this->getuser(false);
        if(empty($user['tel'])||$user['nickname']=="佚名"){
            e("请完善信息",301);
        }

        $user_id=$user['id'];
        $fx_user=model("fxUser")->where("user_id",$user_id)->find();
        if(empty($fx_user)){
            model("fxUser")->insert(['user_id'=>$user_id]);
            $fx_user=model("fxUser")->where("user_id",$user_id)->find();
        }
        $user=model("user")->field("id,nickname,avatar")->where("id",$user_id)->find();
        $up_user=model("user")->field("id,nickname,avatar")->where("id",$fx_user['up_user_id'])->find();
        $fx_user['money']=fxmoney($user_id);
        $fx_user['up_user']=$up_user;
        $fx_user['user']=$user;
        return s($fx_user);
    }

    public function getminicode(){

        $user_id=i("user_id","请传入user_id");
        $app=$this->getwxapp();
        $response = $app->app_code->get("/pages/fx/shop/index?user_id={$user_id}");
        $content=$response->getBodyContents();
        echo $content;
        return response(null)->contentType('Content-Type: image/png');
    }


    public function shop_get(){
        $user_id=$this->getuser();
        $shop_user_id=input("user_id");
        $fx_user=model("fxUser")->where("user_id",$shop_user_id)->find();
        if(empty($fx_user)){
           e("店铺不存在");
        }
        unset($fx_user['up_user_id']);
        unset($fx_user['datetime']);
        if($user_id!=$shop_user_id){
            $fx_user=model("fxUser")->where("user_id",$user_id)->find();
            if(empty($fx_user)){
                model("fxUser")->insert(['user_id'=>$user_id,"up_user_id"=>$shop_user_id]);
            }
        }
        $user=model("user")->field("nickname,avatar")->where("id",$shop_user_id)->find();
        $fx_user['user']=$user;
        return s($fx_user);
    }

    public function talk(){
        $user_id=$this->getuser();
        $money=i("talkmoney","请输入提现金额","float");
        if($money<=0){
            e("提现金额不合法");
        }
        fxmoney($user_id,-$money,"用户发起提现");
        $fx_talk=[];
        $fx_talk['orderno']=getorderno("fxtalk");
        $fx_talk['time']=now();
        $fx_talk['user_id']=$user_id;
        $fx_talk['money']=$money;
        model("fxTalk")->insert($fx_talk);
        return s();

    }
}
